﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using BSCMFF.Core.DataAccessLayer;
using System.Collections.Specialized;

namespace BSCMFF.Core.RoleDataProvider
{
    /// <summary>
    /// Role Provider implemented by Entity Framework
    /// </summary>
    /// <remarks></remarks>
    public class EntityRoleDataProvider : RoleProvider
    {
        /// <summary>
        /// Initializes the provider.
        /// </summary>
        /// <param name="name">The friendly name of the provider.</param>
        /// <param name="config">A collection of the name/value pairs representing the provider-specific attributes specified in the configuration for this provider.</param>
        /// <exception cref="T:System.ArgumentNullException">The name of the provider is null.</exception>
        ///   
        /// <exception cref="T:System.ArgumentException">The name of the provider has a length of zero.</exception>
        ///   
        /// <exception cref="T:System.InvalidOperationException">An attempt is made to call <see cref="M:System.Configuration.Provider.ProviderBase.Initialize(System.String,System.Collections.Specialized.NameValueCollection)"/> on a provider after the provider has already been initialized.</exception>
        /// <remarks></remarks>
        public override void Initialize(string name, NameValueCollection config)
        {

            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (String.IsNullOrEmpty(name))
            {
                name = "EntityRoleDataProvider";
            }

            base.Initialize(name, config);
        }

        /// <summary>
        /// Adds the specified user names to the specified roles.
        /// </summary>
        /// <param name="usernames">A string array of user names to be added to the specified roles.</param>
        /// <param name="roleNames">A string array of the role names to add the specified user names to.</param>
        /// <remarks></remarks>
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets or sets the name of the application to store and retrieve role information for.
        /// </summary>
        /// <value>The name of the application.</value>
        /// <returns>The name of the application to store and retrieve role information for.</returns>
        /// <remarks></remarks>
        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        /// <summary>
        /// Adds a new role to the data source.
        /// </summary>
        /// <param name="roleName">The name of the role to create.</param>
        /// <remarks></remarks>
        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Removes a role from the data source.
        /// </summary>
        /// <param name="roleName">The name of the role to delete.</param>
        /// <param name="throwOnPopulatedRole">If true, throw an exception if <paramref name="roleName"/> has one or more members and do not delete <paramref name="roleName"/>.</param>
        /// <returns>true if the role was successfully deleted; otherwise, false.</returns>
        /// <remarks></remarks>
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets an array of user names in a role where the user name contains the specified user name to match.
        /// </summary>
        /// <param name="roleName">The role to search in.</param>
        /// <param name="usernameToMatch">The user name to search for.</param>
        /// <returns>A string array containing the names of all the users where the user name matches <paramref name="usernameToMatch"/> and the user is a member of the specified role.</returns>
        /// <remarks></remarks>
        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets a list of all the roles.
        /// </summary>
        /// <returns>A string array containing the names of all the roles stored in the data source.</returns>
        /// <remarks></remarks>
        public override string[] GetAllRoles()
        {
            BSCEntities context = new BSCEntities();

            var roles = from r in context.Roles
                        select r;
            int count = roles.Count();
            string[] roleNames = new string[count];
            //List<string> roleNames = new List<string>();
            int i = 0;
            foreach (Role r in roles)
            {
                roleNames[i++] = r.Name;
            }
                        
            return roleNames;
        }

        /// <summary>
        /// Gets a list of the roles that a specified user is in.
        /// </summary>
        /// <param name="username">The user to return a list of roles for.</param>
        /// <returns>A string array containing the names of all the roles that the specified user is in.</returns>
        /// <remarks></remarks>
        public override string[] GetRolesForUser(string username)
        {
            BSCEntities context = new BSCEntities();

            string[] roleName = {(from u in context.Users.Include("Role")
                      where u.Login == username
                      select u.Role.Name).FirstOrDefault()};
            return roleName;            
        }

        /// <summary>
        /// Gets a list of users in the specified role.
        /// </summary>
        /// <param name="roleName">The name of the role to get the list of users for.</param>
        /// <returns>A string array containing the names of all the users who are members of the specified role.</returns>
        /// <remarks></remarks>
        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets a value indicating whether the specified user is in the specified role.
        /// </summary>
        /// <param name="username">The user name to search for.</param>
        /// <param name="roleName">The role to search in.</param>
        /// <returns>true if the specified user is in the specified role; otherwise, false.</returns>
        /// <remarks></remarks>
        public override bool IsUserInRole(string username, string roleName)
        {
            BSCEntities context = new BSCEntities();

            User user = (from u in context.Users.Include("Role")
                           where u.Login == username && u.Role.Name == roleName
                           select u).FirstOrDefault();
            return user == null ? false : true;
        }

        /// <summary>
        /// Removes the specified user names from the specified roles.
        /// </summary>
        /// <param name="usernames">A string array of user names to be removed from the specified roles.</param>
        /// <param name="roleNames">A string array of role names to remove the specified user names from.</param>
        /// <remarks></remarks>
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets a value indicating whether the specified role name already exists in the role data source.
        /// </summary>
        /// <param name="roleName">The name of the role to search for in the data source.</param>
        /// <returns>true if the role name already exists in the data source; otherwise, false.</returns>
        /// <remarks></remarks>
        public override bool RoleExists(string roleName)
        {
            BSCEntities context = new BSCEntities();

            Role role = (from r in context.Roles
                         where r.Name == roleName
                         select r).FirstOrDefault();
            return role == null ? false : true;
        }
    }
}
